package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public String loginPage() {
        return "login";
    }

    @PostMapping("/doLogin")
    public String doLogin(@RequestParam String username, @RequestParam String password, Model model) {
        User user = userService.login(username, password);
        if (user != null) {
            return "redirect:/success"; // 登录成功跳转
        }
        model.addAttribute("error", "用户名或密码错误");
        return "login"; // 登录失败
    }

    @GetMapping("/success")
    public String success(Model model) {
        List<User> users = userService.getAllUsers();
        model.addAttribute("users", users);
        return "success"; // 登录成功页
    }

    // 跳转到注册页
    @GetMapping("/register")
    public String registerPage() {
        return "register";
    }

    @PostMapping("/doRegister")
    public String doRegister(@RequestParam String username, @RequestParam String password, Model model) {
        if (username == null || username.isEmpty() || password == null || password.isEmpty()) {
            model.addAttribute("error", "用户名和密码不能为空");
            return "register";
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        boolean result = userService.register(user);
        if (result) {
            return "redirect:/login"; // 注册成功，跳转到登录页
        }
        model.addAttribute("error", "注册失败，请稍后重试");
        return "register"; // 注册失败
    }

    @GetMapping("/updateUser/{id}")
    public String updateUserPage(@PathVariable Integer id, Model model) {
        User user = userService.findUserById(id);
        if (user == null) {
            return "redirect:/success";
        }
        model.addAttribute("user", user);
        return "updateUser";
    }

    @PostMapping("/doUpdate")
    public String doUpdate(@RequestParam Integer id, @RequestParam String username, @RequestParam String password, Model model) {
        if (id == null || username == null || username.isEmpty() || password == null || password.isEmpty()) {
            model.addAttribute("error", "用户名和密码不能为空");
            return "updateUser";
        }
        User user = new User();
        user.setId(id);
        user.setUsername(username);
        user.setPassword(password);
        boolean result = userService.updateUser(user);
        if (result) {
            return "redirect:/success"; // 更新成功
        }
        model.addAttribute("error", "请稍后重试");
        return "updateUser"; // 更新失败
    }

    @GetMapping("/doDelete/{id}")
    public String doDelete(@PathVariable Integer id) {
        boolean result = userService.deleteUser(id);
        if (result) {
            return "redirect:/success"; // 删除成功
        }
        return "redirect:/success?error"; // 删除失败
    }
}